package com.zjgsu.demo.controller;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.corpus.tag.Nature;
import com.hankcs.hanlp.dictionary.CustomDictionary;
import com.hankcs.hanlp.seg.Segment;
import com.hankcs.hanlp.seg.common.Term;
import com.zjgsu.demo.Repository.DeviceRepository;
import com.zjgsu.demo.nodeEntity.Device;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/rest/v1.0/nlp")
public class NlpController {
    @Autowired
    DeviceRepository deviceRepository;

    //分词控制器
    @GetMapping("/content")
    public Iterable<Device> getDeviceByTitle(@RequestParam(value = "content") String content) {
        List<Device> devices = new ArrayList<Device>();
        try {
            Segment segment = HanLP.newSegment();
            segment.enableCustomDictionary(true);
            CustomDictionary.add("制动衬", "dev 1");
            CustomDictionary.add("限速器", "dev 1");
            CustomDictionary.add("电梯", "dev 1");

            List<Term> seg = segment.seg(content);
            for (Term term : seg) {
                Nature nature = Nature.create("dev");
                if (term.nature == nature) {
                    Device device;
                    System.out.println(term.word);
                    device = deviceRepository.findDeviceByTitle(term.word.toString());
                    if (device != null) {
                        devices.add(device);
                    }
                }
            }

            System.out.println(devices);
        } catch (Exception ex) {
            System.out.println(ex.getClass() + " " + ex.getMessage());
        }
        return  devices;
    }

}